using System;
using System.IO;
using System.Collections;

namespace Patchy
{
	/// <summary>
	/// Summary description for BatchInstall.
	/// </summary>
	public class BatchInstall
	{
        GroupHolder mGroupHolder;
		PatchDetails mDetails;

        public BatchInstall(PatchDetails mDetails, GroupHolder groupHolder)
		{
            mGroupHolder = groupHolder;
			this.mDetails = mDetails;
		}
		
		public new string ToString()
		{
			string patchname, backupdir, makebackupdir, makedestdir, copy2backup = "", copy2dest = "";

            patchname = string.Format("{0}", mDetails.PatchBaseFileName);
            backupdir = @"C:\Backup\%PATCHNAME%\";
            makebackupdir = "";
            foreach (string alias in mGroupHolder.AliasTable.Keys)
            {
                makebackupdir += string.Format("SET {0}={1}\r\n", alias, mGroupHolder.AliasTable[alias].ToString());
            }
            makedestdir = "";
            foreach (string dname in mGroupHolder.DeepestEndDirectories)
            {
                makedestdir += string.Format("IF NOT EXIST \"%BACKUPDIR%{0}\" (\r\n\tECHO %BACKUPDIR%{0}\r\n", dname);
                makedestdir += string.Format("\tMD \"%BACKUPDIR%{0}\"\r\n)\r\n", dname);
            }
            makedestdir += "\r\n\r\n";
            foreach (MyGroup group in mGroupHolder.SortedGroupCollection)
            {
                makedestdir += string.Format("IF NOT EXIST \"{0}\" (\r\n\tECHO {0}\r\n",
                        group.AliasCombined);
                makedestdir += string.Format("\tMD \"{0}\"\r\n)\r\n",
                    group.AliasCombined);
            }
            copy2backup = "";
            foreach (MyGroup group in mGroupHolder.SortedGroupCollection)
            {
                foreach (MyFile file in group.Files)
                {
                    copy2backup += string.Format("IF EXIST \"{0}{1}\" (\r\n\tECHO {2}\r\n",
                        group.AliasCombined,
                        file.FileNamePart,
                        file.ShortenedName);
                    copy2backup += string.Format("\tCOPY \"{0}{1}\" \"%BACKUPDIR%{2}\" /y\r\n)\r\n",
                        group.AliasCombined,
                        file.FileNamePart,
                        file.ShortenedName);
                }
            }
            copy2dest = "";
            foreach (MyGroup group in mGroupHolder.SortedGroupCollection)
            {
                foreach (MyFile file in group.Files)
                {
                    copy2dest += string.Format("ECHO {0}\r\n", file.ShortenedName);
                    copy2dest += string.Format("COPY \"{0}\"{1}\"{2}{3}\" /y\r\n",
                        file.ShortenedName,
                        "".PadRight(file.Padding + 2),
                        group.AliasCombined,
                        file.FileNamePart);
                }
                copy2dest += "\r\n";
            }

            string[] formatArgs = { patchname, backupdir, makebackupdir, makedestdir, copy2backup, copy2dest };
			return string.Format(text, formatArgs);
		}

		private string text = @"@ECHO OFF
CLS

SET PATCHNAME={0}

SET BACKUPDIR={1}

{2}
iisreset /STOP

@ECHO Waiting 3 seconds
PING 1.1.1.1 -n 1 -w 3000 >NUL

@ECHO off
@ECHO ------------------------------------------
@ECHO Create Directories
@ECHO ------------------------------------------

{3}
@ECHO ------------------------------------------
@ECHO Backup Files
@ECHO ------------------------------------------

{4}
@ECHO ------------------------------------------
@ECHO Copy in Patch files
@ECHO ------------------------------------------

{5}@ECHO ------------------------------------------
@ECHO Start Up Service(s)
@ECHO ------------------------------------------

iisreset /START

@ECHO ON
@ECHO ------------------------------------------
@ECHO DONE!
@ECHO To uninstall run ""Uninstall {0}.bat""
@ECHO ------------------------------------------

@PAUSE
";
	}
}
